<template>
	<sw-common-frame :loading="loading">
		<template slot="button" v-if="isExistButton">
			<el-button size="small" type="primary" v-hasPermi="addedButton" icon="el-icon-plus" @click="handleOpenDialog()">新增</el-button>
		</template>
		<el-form slot="query" size="small" inline v-if="isExistQuery">
			<el-form-item>
				<el-input v-model="queryData.name" clearable placeholder="部门名称" style="width: 200px"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" @click="handleQueryData()">查询</el-button>
			</el-form-item>
		</el-form>
		<el-table slot="table" :data="tableListData" v-bind="treeTableConfig" @row-click="handleRowExpanded">
			<el-table-column prop="name" label="部门名称" min-width="300" show-overflow-tooltip></el-table-column>
			<el-table-column prop="createTime" label="创建时间" width="170"></el-table-column>
			<el-table-column label="操作" fixed="right" :width="handleWidth" v-if="isHasAuthButton">
				<template slot-scope="{ row }">
					<el-button type="text" v-hasPermi="handleButtons.addSub" @click.stop="handleAddChildren(row)">添加子级</el-button>
					<el-button type="text" v-hasPermi="handleButtons.update" @click.stop="handleOpenDialog(row.id)">修改</el-button>
					<el-button type="text" v-hasPermi="handleButtons.delete" @click.stop="handleDeleteData(row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<form-dialog v-model="formDialog" :data-id="dataId" :list="tableData" :child.sync="isChildren" @refresh="queryTableData()"></form-dialog>
	</sw-common-frame>
</template>

<script>
import lodash from 'lodash';
import formDialog from './form';
import listMixins from '@/mixins/listMixins';
import { getDepartList, deleteDepart } from '@/api/system/tbSysDept';
export default {
	name: 'departPage',
	mixins: [listMixins],
	components: { formDialog },
	data() {
		return {
			pageName: '部门',
			queryName: ''
		};
	},
	computed: {
		listFunction() {
			return eval(getDepartList);
		},
		deleteFunction() {
			return eval(deleteDepart);
		},
		treeTableConfig() {
			let config = { ...this.tableConfig };
			config['treeProps'] = { children: 'children', hasChildren: 'hasChildren' };
			config['defaultExpandAll'] = true;
			return { ...config, rowKey: 'id' };
		},
		frameTopButton() {
			let frameTopBtns = [];
			if (!!this.addedButton) {
				frameTopBtns = [...frameTopBtns, this.addedButton];
			}
			return frameTopBtns;
		},
		requestParams() {
			return {};
		},
		tableListData() {
			let name = this.queryName.toLowerCase();
			let tableData = lodash.cloneDeep(this.tableData);
			let tableSortData = lodash.sortBy(tableData, 'sort');
			let filterTreeData = (nodes) => {
				let filtered = [];
				nodes.forEach((node) => {
					if (node.name.toLowerCase().includes(name)) {
						let child = node.children ? filterTreeData(node.children) : [];
						child.length ? filtered.push({ ...node, children: child }) : filtered.push(node);
					} else if (node.children) {
						let children = filterTreeData(node.children);
						children.length && filtered.push({ ...node, children });
					}
				});
				return filtered;
			};
			return filterTreeData(this.$flatToTree(tableSortData, 'pid'));
		}
	},
	watch: {
		queryName() {
			this.$refs['table'].doLayout();
		}
	},

	methods: {
		//查询部门
		handleQueryData() {
			this.queryName = lodash.cloneDeep(this.queryData.name);
		},

		//删除部门后更新STORE
		updateStoreData() {
			this.$store.dispatch('getDepartList');
		}
	}
};
</script>
